import axios from 'axios';
import CryptoJS from 'crypto-js';
// 云市场分配的密钥Id
let secretId = "AKID6otlg1m8hgin41eihckt2Sjw7fh7a38xgdTK";
// 云市场分配的密钥Key
let secretKey = "jmqxA0fKjJlz0xUI44S65D5hjQ9DaV5s3tfBT7um";
let source = "market";

// 签名
let datetime = (new Date()).toGMTString();
let signStr = "x-date: " + datetime + "\n" + "x-source: " + source;
let sign = CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA1(signStr, secretKey))
let auth = 'hmac id="' + secretId + '", algorithm="hmac-sha1", headers="x-date x-source", signature="' + sign + '"';

let headers = {
    "X-Source": source,
    "X-Date": datetime,
    "Authorization": auth,
}
// axios.defaults.headers = headers
//配置跨域
axios.defaults.crossDomain = false;
//跨域安全策略
axios.defaults.withCredentials = false;

axios.defaults.transformRequest = [function (data) {
    if (data instanceof FormData) {
        return data;
    }
    data = JSON.stringify(data);
    return data;
}];

axios.interceptors.request.use(
    config => {
        //store.dispatch('loading', true);
        if(config.url === "https://service-iw45ygyx-1255468759.ap-beijing.apigateway.myqcloud.com/release/view")
            config.headers = headers
        return config;
    },
    err => {
        //store.dispatch('loading', false);
        return Promise.reject(err);
    }
);

axios.interceptors.response.use((response) => {

    //store.dispatch('loading', false);
    switch (response.data.code){
        case '500':

            break;
    }
    return response;
}, (error) => {
    return Promise.reject(error)
});

// 统一发起请求的函数
export default function request(options) {
    return axios.request(options);
}

